
package editor;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.awt.font.NumericShaper;
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.FilterWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.io.StringWriter;
import java.text.Format;
import java.util.Enumeration;
import java.util.Vector;

import javax.swing.DefaultListModel;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JComboBox;
import javax.swing.JFormattedTextField;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextField;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
import javax.swing.text.AttributeSet;
import javax.swing.text.BadLocationException;
import javax.swing.text.Document;
import javax.swing.text.PlainDocument;

import datatypes.Kategorie;
import datatypes.Patient;
import datatypes.PatientTemplate;

public class PatientEditor extends JPanel implements ActionListener, ListSelectionListener, ItemListener {

	private static final long serialVersionUID = 5180758911735707023L;

	JList<PatientTemplate> cb_patientenbox;
	DefaultListModel<PatientTemplate> dlm_patientenbox;
	JTextField tf_diagnose;
	JTextField tf_min_status;
	JTextField tf_max_status;
	JCheckBox cb_napflicht;
	JCheckBox cb_krhpflicht;
	JCheckBox cb_gehfaehig;
	JCheckBox cb_tragehilfe;
	JComboBox<Kategorie> cb_kategorie;

	JButton btn_save;
	JButton btn_update;

	int currentIndex = 0;
	//boolean currentDataChanged = false;

	private void createGUIelements() {
		dlm_patientenbox = new DefaultListModel<PatientTemplate>();
		//dlm_patientenbox.addElement(new PatientTemplate(0, 0, "Neuer Patient", null, false, false, false, false));
		
		cb_patientenbox = new JList<PatientTemplate>(dlm_patientenbox);
		cb_patientenbox.addListSelectionListener(this);

		JScrollPane sp_patientenbox = new JScrollPane(cb_patientenbox);

		this.add(sp_patientenbox, BorderLayout.LINE_START);

		tf_diagnose = new JTextField(50);

		tf_min_status = new JTextField();
		tf_min_status.setDocument(createNumericDocumentWithMaxLength(2));
		tf_max_status = new JTextField();
		tf_max_status.setDocument(createNumericDocumentWithMaxLength(2));

		cb_gehfaehig = new JCheckBox();
		cb_gehfaehig.addItemListener(this);
		cb_krhpflicht = new JCheckBox();
		cb_krhpflicht.addItemListener(this);
		cb_napflicht = new JCheckBox();
		cb_napflicht.addItemListener(this);
		cb_tragehilfe = new JCheckBox();
		cb_tragehilfe.addItemListener(this);
		cb_kategorie = new JComboBox<Kategorie>(Kategorie.values());

		JPanel panel = new JPanel(new GridLayout(7, 2));

		panel.add(new JLabel("Diagnose"));
		panel.add(tf_diagnose);
		panel.add(tf_min_status);
		panel.add(tf_max_status);
		panel.add(new JLabel("Notarztpflichtig?"));
		panel.add(cb_napflicht);
		panel.add(new JLabel("Krankenhauspflichtig?"));
		panel.add(cb_krhpflicht);
		panel.add(new JLabel("Gehfaehig?"));
		panel.add(cb_gehfaehig);
		panel.add(new JLabel("Tragehilfe noetig?"));
		panel.add(cb_tragehilfe);
		panel.add(cb_kategorie);

		btn_update = new JButton("Speichern");
		btn_update.addActionListener(this);
		panel.add(btn_update);

		panel.setPreferredSize(panel.getPreferredSize());

		this.add(panel, BorderLayout.CENTER);
		
		btn_save = new JButton("In Datei speichern");
		btn_save.addActionListener(this);
		this.add(btn_save, BorderLayout.PAGE_END);
	}

	private Document createNumericDocumentWithMaxLength(final int maxLength) {
		Document doc = new PlainDocument() {
			@Override
			public void insertString(int offs, String str, AttributeSet a) throws BadLocationException {
				if (getLength() + str.length() > maxLength)
					return;
				if (!str.matches("^[\\d]*$"))
					return;

				super.insertString(offs, str, a);
			}
		};
		return doc;
	}

	public PatientEditor() {
		super(new BorderLayout());
		createGUIelements();

	}

	@Override
	public void actionPerformed(ActionEvent arg0)
	{
		if(arg0.getSource() == btn_update)
		{
			updatePatient();
		}
		else if(arg0.getSource() == btn_save)
		{
			savePatientsToFile();
		}
	}

	private void updatePatient()
	{
		System.out.println("update patient");
		
		PatientTemplate p = new PatientTemplate(Integer.parseInt(tf_min_status.getText()), Integer.parseInt(tf_max_status.getText()), tf_diagnose.getText(), (Kategorie) cb_kategorie.getSelectedItem() , cb_napflicht.isSelected(), cb_krhpflicht.isSelected(), cb_gehfaehig.isSelected(), cb_tragehilfe.isSelected());
		
		if(p.isValid())
		{
			if((cb_patientenbox.getSelectedIndex()!=-1)&& !cb_patientenbox.getSelectedValue().getDiagnose().contentEquals("Neuer Patient"))
			{	
				dlm_patientenbox.set(cb_patientenbox.getSelectedIndex(), p);
			}
			else
			{
				dlm_patientenbox.addElement(p);
			}
		}
	}
	
	private void savePatientsToFile()
	{
		
		try {
			FileWriter fw = new FileWriter("wurstsalat.txt");
			PrintWriter pw = new PrintWriter(fw);
			
			Enumeration<PatientTemplate> patients = dlm_patientenbox.elements();
			
			while(patients.hasMoreElements())
			{
				pw.println(patients.nextElement().toSerialisationString());
			}
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
	}
	
	@Override
	public void valueChanged(ListSelectionEvent arg0) {
		
		PatientTemplate pt = dlm_patientenbox.elementAt(cb_patientenbox.getSelectedIndex());
		if(!cb_patientenbox.getSelectedValue().getDiagnose().contentEquals("Neuer Patient"))
		{
			tf_diagnose.setText(pt.getDiagnose());
		}
		else
		{
			tf_diagnose.setText("");
		}
		tf_min_status.setText(String.valueOf(pt.getMin_status()));
		tf_max_status.setText(String.valueOf(pt.getMax_status()));
		cb_napflicht.setSelected(pt.isNaPflichtig());
		cb_krhpflicht.setSelected(pt.isKrhPflichtig());
		cb_tragehilfe.setSelected(pt.benTragehilfe());
		cb_gehfaehig.setSelected(pt.isGehfaehig());
		cb_kategorie.setSelectedItem(pt.getKategorie());

		//currentDataChanged = false;
	}

	@Override
	public void itemStateChanged(ItemEvent arg0) {
		//updatePatient();
	}
}
